/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.test.login_lejingling_cjl.controller;

import com.test.login_lejingling_cjl.dto.Result;
import com.test.login_lejingling_cjl.dto.loginDTO;
import com.test.login_lejingling_cjl.entity.User;
import com.test.login_lejingling_cjl.service.UserService;
import com.test.login_lejingling_cjl.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private  UserService userService;


    // http://127.0.0.1:8080/hello?name=lisi
    @GetMapping("/login")
    @ResponseBody
    public Result login(@RequestBody loginDTO loginDTO) {
        log.info("用户登录");
        User e=userService.login(loginDTO);
        //e!=null表示登录成功（生成，校验），如何获取前端发来的jwt令牌进行性校验  答：似乎校验的功能并不是在登录上处理的
        if(e!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",e.getPhone()); //暂时使用phone,后续应该添加id字段
            String jwt= JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或者密码输入错误");
    }


}
